package org.i_plus.model.services;

import java.util.List;

import org.hibernate.SessionFactory;
import org.i_plus.model.MapBean;
import org.i_plus.model.dao.MapDAOHibernate;
import org.i_plus.model.dao.Map_ReplyDAOHibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MapService {
	private MapDAOHibernate dao = null;
	private Map_ReplyDAOHibernate mdao = null;
	
	public MapService(MapDAOHibernate dao) {
		this.dao = dao;
	}
	public void setMap_ReplyDAO(Map_ReplyDAOHibernate mdao){
		this.mdao = mdao;
	}//第一步 加這段 第二步 改beans.config.xml

	public MapBean insert(String account, String m_content, double x_axis,
			double y_axis, String m_title, byte[] m_pic) {

		MapBean bean = new MapBean();
		bean.setAccount(account);
		bean.setM_content(m_content);
		bean.setX_axis(x_axis);
		bean.setY_axis(y_axis);
		bean.setM_title(m_title);
		bean.setM_pic(m_pic);
		dao.insertM(account, m_content, x_axis, y_axis, m_title, m_pic);
		return bean;

	}

	public MapBean update(String account, int m_num, String m_content,
			double x_axis, double y_axis, String m_title, byte[] m_pic) {

		MapBean bean = new MapBean();
		bean.setAccount(account);
		bean.setM_num(m_num);
		bean.setM_content(m_content);
		bean.setX_axis(x_axis);
		bean.setY_axis(y_axis);
		bean.setM_title(m_title);
		bean.setM_pic(m_pic);
		dao.updateM(account, m_num, m_content, x_axis, y_axis, m_title, m_pic);
		return bean;

	}

	public void delete(int m_num) {
		dao.deleteM(m_num);

	}
	public List<MapBean> selectAll(){
		return dao.selectAll();
	}
	public MapBean selectByPK(int m_num){
		MapBean bean = dao.selectM(m_num);//這時候的MapBean 已裝滿map table內的資料 但還沒裝map_reply的資料
		bean.setMrbean(mdao.selectMany(m_num));//這段就是將table map_reply的資料裝進MapBean
		return bean;//這時候的bean已裝滿table map 及 map_reply的資料
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.config.xml");
		MapService service = (MapService) context
				.getBean("mapService");
		SessionFactory sessionFactory = (SessionFactory) context
				.getBean("sessionFactory");
		sessionFactory.getCurrentSession().beginTransaction();
		//以下為測試程式
		
//		MapBean bean = service.selectByPK(2);
//		System.out.println(bean.getAccount());
//		System.out.println(bean.getM_title());
//		for(Map_ReplyBean temp:bean.getMrbean()){
//			System.out.println(temp.getM_re_time());
//		}   //selectByPK
		
//		MapBean bean1 = service.insert("BB", "jll", 12.1 , 15.4 , "dfg" , null);
//		System.out.println("done");   //insert
		
		List<MapBean> bean = service.selectAll();
//		System.out.println("done");   //selectAll
		
//		MapBean bean = service.update("BB",12, "jon", 12.1 , 19.9 , "dfg" , null);
//		System.out.println("done");  //依主鍵  int m_num 來update 
		
//		service.delete(9);
		System.out.println("done"); //delete 無回傳值直接呼叫即可
	
		
		
		//以上為測試程式
		sessionFactory.getCurrentSession().getTransaction().commit();

	}

}
